package com.zbbt.sportslottery.ui.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;

import com.gc.materialdesign.views.ProgressBarCircularIndetermininate;
import com.google.gson.JsonObject;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.bean.ActionCenter;
import com.zbbt.sportslottery.bean.SuperResult;
import com.zbbt.sportslottery.net.TypeGsonHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.ui.adapter.ActionCenterAdapter;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout.OnRefreshListener;

/**
 * 
 * @ClassName: ActionCenterFragment
 * @Description: 活动中心
 * @author：LiZhimin
 * @date：2014-12-12 下午4:35:41
 * @version V1.0
 */
public class ActionCenterActivity extends SimpleBaseActivity {
	private SwipeRefreshLayout mSwipeRefreshLayout;
	private ProgressBarCircularIndetermininate mProgressBar;
	private ListView mListView;
	private List<ActionCenter> dataList = new ArrayList<ActionCenter>();
	private ActionCenterAdapter adapter;
	private boolean hasMore = false;// 是否有下一页数据
	private int pageSize = 10;
	private int curPage = 1;

	public ActionCenterActivity() {}

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.fragment_center);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mListView = (ListView) findViewById(R.id.listview_center);
		mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
		mProgressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.progressBarCircularIndetermininate);
		adapter = new ActionCenterAdapter(context, dataList, handler);
		mListView.setAdapter(adapter);
		mProgressBar.setVisibility(View.VISIBLE);
		httpData(Constant.REFRESH);
	}

	public static ActionCenterActivity getInstance() {
		return new ActionCenterActivity();

	}

	@Override
	protected void setListener() {
		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				curPage = 1;
				httpData(Constant.REFRESH);
				mSwipeRefreshLayout.setRefreshing(false);
			}
		});
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
//		if (App.citychange == true) {
//			httpData(Constant.REFRESH);
//		}
	}
	private void httpData(final int loadType) {
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("curPage", curPage);
		jsonObject.addProperty("pageSize", pageSize);
		ZbHttpClient.postNewApi(ServerConfig.API_ACTION_LIST, jsonObject,
				new TypeGsonHttpHandler<SuperResult<ActionCenter>>(true) {

					@Override
					public void onSuccess(SuperResult<ActionCenter> data) {
						if (data != null) {
							ArrayList<ActionCenter> resultList = data.getData();
							if (loadType == Constant.REFRESH) {
								dataList.clear();
							}
							if (data.getTotalPage() == data.getCurPage()) {
								hasMore = false;
							} else {
								hasMore = true;
							}
							dataList.addAll(resultList);
							adapter.notifyDataSetChanged();
							mProgressBar.setVisibility(View.GONE);
						}
					}

					@Override
					public void onFailure(String errorCode, String errorMsg) {
						hasMore = false;
						mProgressBar.setVisibility(View.GONE);
					}
				});
	}

	public Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Constant.HAS_MORE:
				if (hasMore == true) {
					curPage++;
					httpData(Constant.HAS_MORE);
				}
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onClickEvent(View v) {

	}

	@Override
	protected void initData() {

	}

}
